<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Admin_Serviceoptions extends Admin_Controller {
	public function __construct()
	{
		parent::__construct();
		$this->lang->load('serviceoption');
	}
	public function ajax_serviceoption_get(){
		$serviceoptionEntity =  new CI_Service_option_entity();
		$serviceoption = $serviceoptionEntity->getServiceOptionByCode($_REQUEST['code']);
		echo json_encode($serviceoption);
	}
	public function ajax_serviceoption_list(){
		$serviceoptionEntity =  new CI_Service_option_entity();
		$escortSettings = new CI_Escort_setting_entity();
		$defaultLanguageId = $escortSettings->getDefaultLanguageId();
		$serviceoptionList = $serviceoptionEntity->getServiceOptionListByLanguage($defaultLanguageId);
		$data['serviceoptionList'] = $serviceoptionList;
		
		$languageEntity = new CI_Language_entity();
		$languageList = $languageEntity->getLanguageList();
		$data['languageList'] = $languageList;
		
		$this->load->view('admin/serviceoptions/index', $data);
	}
	public function ajax_serviceoption_save(){
		$serviceoptionNameList = $_REQUEST['serviceoption'];
		$serviceoptionNameDefault = '';
		foreach ($serviceoptionNameList as $key => $value) {
			if($value != null && $value != ""){
				$serviceoptionNameDefault = $value;
				break;
			}
		}
		$result = array();
		if($serviceoptionNameDefault == ''){
			$result['result'] = false;
			$result['message'] = lang('escort_serviceoption_require');
		}else{
			$code = 0;
			foreach ($serviceoptionNameList as $key => $value) {
				$serviceoptionEntity =  new CI_Service_option_entity();
				if($value != null && $value != ''){
					$serviceoptionEntity->setService($value);
				}else{
					$serviceoptionEntity->setService($serviceoptionNameDefault);
				}
				$serviceoptionEntity->setLanguageId($key);
				$serviceoptionEntity->setCode($code);
				$id = $serviceoptionEntity->save();
				if($code == 0){
					$code = $id;
					$serviceoptionEntity->setId($id);
					$serviceoptionEntity->setCode($code);
					$serviceoptionEntity->update();
				}
			}
			$result['result'] = true;
			$result['message'] =lang('escort_serviceoption_save_success');
			$result['id'] =$id;
		}
		echo json_encode($result);
	}
	public function ajax_serviceoption_update(){
		$serviceoptionNameList = $_REQUEST['serviceoption'];
		$serviceoptionIdList = $_REQUEST['serviceoption_id'];
		$serviceoptionCode = $_REQUEST['serviceoption_code'];
		$serviceoptionNameDefault = '';
		foreach ($serviceoptionNameList as $key => $value) {
			if($value != null && $value != ""){
				$serviceoptionNameDefault = $value;
				break;
			}
		}
		$result = array();
		if($serviceoptionNameDefault == ''){
			$result['result'] = false;
			$result['message'] = lang('escort_serviceoption_require');
		}else{
			foreach ($serviceoptionNameList as $key => $value) {
				$serviceoptionEntity =  new CI_Service_option_entity();
				if($value != null && $value != ''){
					$serviceoptionEntity->setService($value);
				}else{
					$serviceoptionEntity->setService($serviceoptionNameDefault);
				}
				$serviceoptionEntity->setLanguageId($key);
				$serviceoptionEntity->setCode($serviceoptionCode);
				$serviceoptionEntity->setId($serviceoptionIdList[$key]);
				$serviceoptionEntity->update();
			}
			$result['result'] = true;
			$result['message'] =lang('escort_serviceoption_save_success');
		}
		
		echo json_encode($result);
	}
	public function ajax_serviceoption_delete(){
		$result = array();
		$serviceoptionEntity =  new CI_Service_option_entity();
		$result['result'] = $serviceoptionEntity->deleteByCode($_REQUEST['code']);
		if($result['result']){
			$result['message'] = lang('escort_serviceoption_delete_success');
		}else{
			$result['message'] = lang('escort_serviceoption_delete_error');
		}
		echo json_encode($result);
	}
}
?>